package com.pudu.mydemo.act;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import com.pudu.mydemo.utils.WiFi;
import com.pudu.mydemo.view.AnimTextView;
import com.pudu.mydemo.view.CircleImageView;
import com.pudu.mydemo.view.MagicProgressCircle;
import com.pudutech.pdrobot.R;


/**
 * Created by guiliu on 2016/7/11.
 */
public class SmartActivity extends BaseHomeActivity {


    private ImageView cancel,ok;
    private ImageView route;
    private MagicProgressCircle demoMpc;
    private AnimTextView demoTv;
    private int requestCode = 0;
    private boolean isActive;
    private int map = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    protected int getLayoutId() {
        return R.layout.smart_curise;
    }

    @Override
    public void initView() {

        demoMpc = (MagicProgressCircle) findViewById(R.id.demo_mpc);
        demoTv = (AnimTextView) findViewById(R.id.demo_tv);
        cancel = (ImageView)findViewById(R.id.cancel);
        ok=(ImageView)findViewById(R.id.ok);
        route=(ImageView) findViewById(R.id.route_select);
        demoMpc.setStartColor(Color.GRAY);
        demoMpc.setEndColor(Color.WHITE);

    }


    @Override
    protected void onResume() {
        super.onResume();
        startAnimation();
      /*  if(WiFi.isWifiOpen()) {
            startAnimation();
        } else {
            Intent intent = new Intent(SmartActivity.this, VideoActivity.class);
            startActivity(intent);
        }*/

    }

    private AnimatorSet set;
    public void startAnimation() {
        {

            /*final AnimatorSet*/ set = new AnimatorSet();
            set.playTogether(
                    ObjectAnimator.ofFloat(demoMpc, "percent", 0, 100 / 100f),
                    ObjectAnimator.ofInt(demoTv, "progress",05, 0)
            );
            set.setDuration(6000);
            set.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    isActive = true;
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if(isActive) {
                        launchActivity();
                        isActive = false;
                    }
                    Log.d("guiliu","end");

                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    Log.d("guiliu","cancel");
                    isActive = false;
                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            set.setInterpolator(new AccelerateInterpolator());
            set.start();

            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    set.pause();
                    finish();
                }
            });

            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    set.resume();
                    launchActivity();
                }
            });

           /* route.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SmartActivity.this,RouteSelectActivity.class);
                    set.pause();
                    startActivityForResult(intent,requestCode);

                }
            });*/
            route.setOnClickListener(this);

        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        set.cancel();
    }

    public void launchActivity(){
        Intent intent = new Intent();
        intent.setAction("Intent_Action_Move");
        intent.putExtra("action","ACTION_SMART_MOVE");
        intent.putExtra("data",map+"");
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if(requestCode == this.requestCode) {
            map = intent.getIntExtra("position",0);
            //int position = intent.getExtra("position", 1);
           // Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_LONG).show();
        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v == route){
            Intent intent = new Intent(SmartActivity.this,RouteSelectActivity.class);
            set.pause();
            startActivityForResult(intent,requestCode);
        }
    }


    @Override
    public String getClickAction(View v) {
        if(v == route) {
            return "选择路线";
        }
        return super.getClickAction(v);
    }
}


